/* make new directory */
local dir `c(pwd)'
capture mkdir length_validation_II_e


local dir `c(pwd)'
use entropy_bible, clear
merge m:1 trans using data_bible_final, keep(3)

keep if book==40|book==41|book==42|book==43|book==44|book==66
/*keep only translations with N=6 available books*/
bysort trans: gen howmany=_N
keep if howmany==6
drop howmany
/*calculate pctile(25) number of words per book */
bysort book: egen pctile=pctile(words_original),p(25)
foreach book in 40 41 42 43 44 66		{	
		sum pctile if book==`book' 
		gen pctile`book'=r(mean)
	}
bysort trans: gen id=_n
keep if id==1
keep trans language latitude longitude family ISO pctile* book

gen random=runiform()

	egen group = cut(random), group(`1') label
	
sum group
local min=r(min)
local max=r(max)

forvalues g=`min'/`max' {
	preserve
	keep if group==`g'
	save "`dir'\bible_index_`g'", replace
	/************ insert the working directory of Stata here ************/
	winexec C:\Program Files (x86)\Stata14\StataMP-64 -q do `dir'\length_validation_II_e.do `g'
	restore
}

clear         
/* wait until everything is finished */
clear
 forvalues g=`min'/`max' {  
   capture confirm file finished_`g'.dta
   while _rc != 0 {
      sleep 2000
      capture confirm file finished_`g'.dta
   }
 }

forvalues g=`min'/`max' {
	capture erase finished_`g'.dta
}

exit
